/**
 *	Contains the win checker object and code
 *
 *	each function in here returns a number out of the following 4 values:
 *		-1	:	game ended in a draw
 *		0	:	game ongoing
 *		1	:	player 1 win
 *		2	:	player 2 win
**/

"use strict"

if (typeof GameLogic === "undefined") {
	var GameLogic = { }
}

GameLogic.winChecker = {
	
	//main entry into the checker
	check: function(game) {
		this.game = game
		
		var ruleorder = [									//make sure rules are appraised in order
			"checkAllSquaresFull",
			"checkFullRow",
			"checkFullColumn",
			"checkFullDiagonals"
		]
		var result
		
		for (var i=0, ii=ruleorder.length; i<ii; i++) {		//loop through all rules and check for a winner
			result = this[ ruleorder[i] ]()					//run the rule
			if (result !== 0)  return result				//return a winner if found
		}
		return 0											//no winner, return 0 (game in progress)
	},
	
	
	checkAllSquaresFull: function() {
		var state = this.game.state
		for (var y=0, yy=state.length; y<yy; y++) {
			for (var x=0, xx=state[0].length; x<xx; x++) {
				 if (state[y][x] == 0)  return 0			//0 signifies no winner found
			}
		}
		return -1											//-1 signifies a draw
	},
	
	checkFullRow: function() {
		var state = this.game.state, rowMatch
		
		for (var y=0, yy=state.length; y<yy; y++) {
			rowMatch = true
			for (var x=0, xx=state[0].length; x<xx; x++) {
				rowMatch &= ( 
					state[y][0] !== 0 &&					//if first item is 0, line must not be full
					state[y][0] === state[y][x]				//match first item on the line with all other items in the row
				)
			}
			if (rowMatch == true)  return state[y][0]		//if true, return winning player. first line item must signify the winner!
		}
		return 0											//must be no matching rows, so return 0 (game still in progress)
	},
	
	checkFullColumn: function() {
		var colMatch, firstColItem,
			state = this.game.state,
			w = this.game.board.w,
			h = this.game.board.h
		
		for (var x=0; x<w; x++) {
			colMatch = true
			for (var y=0; y<h; y++) {
				firstColItem = state[0][x]					//value of the first item on the column
				colMatch &= ( 
					firstColItem !== 0 &&					//if first column item is 0, vertical line must not be full
					firstColItem === state[y][x]			//match first item on the line with all other items in the row
				)
			}
			if (colMatch == true)  return firstColItem		//if true, return winning player. first column item must signify the winner!
		}
		return 0											//must be no matching cols, so return 0 (game still in progress)
	},
	
	//checks both diagonals, and returns the winner if either are full
	checkFullDiagonals: function() {
		var diagMatch, firstDiagItem, 
			state = this.game.state,
			w = this.game.board.w,
			h = this.game.board.h
		
		firstDiagItem = state[0][0]							//diagonal top-left -> bottom-right
		diagMatch = true
		for (var i=0; i<w; i++) {							//note: assumes gameboard is square
			diagMatch &= (
				state[i][i] !== 0 && 
				state[i][i] === firstDiagItem
			)
		}
		if (diagMatch == true)  return firstDiagItem
		
		var hh = h-1										//height limit
		firstDiagItem = state[hh][0]						//diagonal bottom-left -> top-right
		diagMatch = true
		for (var i=0; i<w; i++) {
			diagMatch &= (
				state[hh-i][i] !== 0 && 
				state[hh-i][i] === firstDiagItem
			)
		}
		if (diagMatch == true)  return firstDiagItem
		
		return 0											//if no match found by now, none must exist diagonally
	}
	
}
